package com.winglish.as3.exam.toeicspeaking 
{
	import com.winglish.as3.exam.toeicspeaking.vo.TSAnswerVo;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import com.winglish.as3.utils.StringUtil;
	import com.winglish.as3.ui.WScrollBar;
	import com.winglish.as3.utils.ObjectUtil;
	
	
	/**
	 * ...
	 * @author charmjin
	 */
	
	public class TSAnswer extends Sprite
	{
		protected var dataObj:Object;
		protected var _root:MovieClip;
		protected var partNo:int;
		private var ansSound:Array;
		private var record:Array;
		private var ansSoundSec:Array;	//이어듣기 초 저장
		private var recordSec:Array;	//이어듣기 초 저장
		
		public function TSAnswer(mc:MovieClip = null, data:Object = null, no:int = 0)
		{
			if (mc && data) {
				dataObj = data;
				_root = mc;
				partNo = no;
				ansSoundSec = new Array();
				recordSec = new Array();
			}
		}
		
		/**
		 * 피드백 텍스트 셋팅.
		 */
		protected function answerSet():void {
			for (var i:uint = TSData.PART_QST_CNT[partNo - 1]; i < TSData.PART_QST_CNT[partNo]; i++) {
				var no:uint = i - TSData.PART_QST_CNT[partNo - 1];
				var mc:MovieClip = _root.mcRoot["q" + no] as MovieClip;
				if (mc) {
					mc.qusNo.gotoAndStop(i);
					mc.scriptMc.gotoAndStop(2);
					mc.scriptMc.buttonMode = true;
					mc.translationMc.buttonMode = true;
					scriptSet(mc, no);
					soundSet(mc);
				}
				recordSec[no] = 0;
			}
			
			_root.reStartBtn.addEventListener(MouseEvent.CLICK, reStartBtnClick);
		}
		
		/**
		 * translation btn click event
		 * @param	e
		 */
		private function translationMcClick(e:MouseEvent):void {
			var mc:MovieClip = e.currentTarget.parent as MovieClip;
			var no:uint = uint(mc.name.substr( -1));
			e.currentTarget.gotoAndStop(2);
			mc.scriptMc.gotoAndStop(1);
			if (mc) {
				if (mc.scriptMc.hasEventListener(MouseEvent.CLICK)) mc.scriptMc.removeEventListener(MouseEvent.CLICK, scriptMcClick);
				mc.gotoAndStop(2);
				translationSet(mc, no);
			}
		}
		
		/**
		 * script btn click event
		 * @param	e
		 */
		private function scriptMcClick(e:MouseEvent):void {
			var mc:MovieClip = e.currentTarget.parent as MovieClip;
			var no:uint = uint(mc.name.substr( -1));
			e.currentTarget.gotoAndStop(2);
			mc.translationMc.gotoAndStop(1);
			if (mc) {
				if (mc.translationMc.hasEventListener(MouseEvent.CLICK)) mc.translationMc.removeEventListener(MouseEvent.CLICK, translationMcClick);
				mc.gotoAndStop(1);
				scriptSet(mc, no);
			}
		}
		
		/**
		 * script text set
		 * @param	mc
		 * @param	no
		 */
		private function scriptSet(mc:MovieClip, no:uint):void {
			var contents:MovieClip = mc.ansMc.content;
			contents.txt.autoSize = TextFieldAutoSize.LEFT;
			contents.txt.mouseWheelEnabled = false;
			contents.txt.text = dataObj["quiz" + no].script;
			
			switch(TSData.IS_VOCA_TIP[partNo - 1]) {
				case "v" : //voca
					if (StringUtil.replaceEmpty(dataObj["quiz" + no].voca) != "") {
						contents.lineMc.y = contents.txt.x + contents.txt.height + 15;
						contents.vocaMc.y = contents.lineMc.y + 15;
						contents.vocaTxt.y = contents.vocaMc.y + contents.vocaMc.height + 5;
						
						contents.vocaTxt.mouseWheelEnabled = false;
						contents.vocaTxt.autoSize = TextFieldAutoSize.LEFT;
						contents.vocaTxt.multiline = true;
						contents.vocaTxt.text = StringUtil.strReplace(dataObj["quiz" + no].voca,"|","\n");
						StringUtil.korChangeFormat(contents.vocaTxt, {color:0x333333, size:12});
						
					}else {
						contents.vocaMc.visible = contents.lineMc.visible = false;
					}
					break;
				case "t" :  //tip
					if (StringUtil.replaceEmpty(dataObj["quiz" + no].tip) != "") {
						contents.lineMc.y = contents.txt.x + contents.txt.height + 15;
						contents.tipMc.y = contents.lineMc.y + 15;
						contents.tipTxt.y = contents.tipMc.y + contents.tipMc.height + 5;
						
						contents.tipTxt.mouseWheelEnabled = false;
						contents.tipTxt.autoSize = TextFieldAutoSize.LEFT;
						contents.tipTxt.multiline = true;
						contents.tipTxt.text = StringUtil.strReplace(dataObj["quiz" + no].tip,"|","\n");;
					}else {
						contents.tipMc.visible = contents.lineMc.visible = false;
					}
					break;
			}
			
			var _wsb:WScrollBar = WScrollBar.GET( mc.ansMc, { width: TSData.SCROLL_WIDTH[partNo - 1], height: TSData.SCROLL_HEIGHT[partNo - 1], offset: 10 } );
			
			if (!mc.translationMc.hasEventListener(MouseEvent.CLICK)) mc.translationMc.addEventListener(MouseEvent.CLICK, translationMcClick);
		}
		
		/**
		 * translation text set
		 * @param	mc
		 * @param	no
		 */
		private function translationSet(mc:MovieClip, no:uint):void {
			var contents:MovieClip = mc.ansMc.content;
			contents.txt.multiline = true;
			contents.txt.autoSize = TextFieldAutoSize.LEFT;
			contents.txt.mouseWheelEnabled = false;
			contents.txt.text = StringUtil.strReplace(dataObj["quiz" + no].translation, "|", "\n");
			
			var _wsb:WScrollBar = WScrollBar.GET( mc.ansMc, { width: TSData.SCROLL_WIDTH[partNo - 1], height: TSData.SCROLL_HEIGHT[partNo - 1], offset: 10 } );
			
			if (!mc.scriptMc.hasEventListener(MouseEvent.CLICK)) mc.scriptMc.addEventListener(MouseEvent.CLICK, scriptMcClick);
		}
		
		/**
		 * 나의 답변, 모범 답변 사운드 셋팅.
		 * @param	ansArr
		 * @param	recArr
		 */
		public function soundRecordSet(ansArr:Array, recArr:Array):void {
			if (ansArr.length > 0) ansSound = ansArr;
			if (recArr.length > 0) record = recArr;
		}
		
		/**
		 * 나의 답변, 모범 답변 컨트롤 버튼 셋팅.
		 * @param	mc
		 */
		private function soundSet(mc:MovieClip):void {
			var type:String = TSData.ANSWER_SYMBOL_TYPE;
			if (type == "A") {
				mc.mySoundBtn.addEventListener(MouseEvent.CLICK, mySoundBtnClick);
				mc.mySoundStopBtn.addEventListener(MouseEvent.CLICK, soundStopBtnClick);
				mc.ansBtn.addEventListener(MouseEvent.CLICK, ansBtnClick);
				mc.ansStopBtn.addEventListener(MouseEvent.CLICK, soundStopBtnClick);
			}else if (type == "B") {
				mc.mySoundMc.buttonMode = true;
				mc.mySoundMc.addEventListener(MouseEvent.CLICK, soundMcClick);
				mc.mySoundStopMc.buttonMode = true;
				mc.mySoundStopMc.addEventListener(MouseEvent.CLICK, soundPauseMcClick);
				//mc.mySoundStopMc.gotoAndStop(2);
				mc.ansSoundMc.buttonMode = true;
				mc.ansSoundMc.addEventListener(MouseEvent.CLICK, soundMcClick);
				mc.ansSoundPauseMc.buttonMode = true;
				mc.ansSoundPauseMc.addEventListener(MouseEvent.CLICK, soundPauseMcClick);
				//mc.ansSoundPauseMc.gotoAndStop(2);
			}
			
		}
		
		/**
		 * 나의 답변 btn click
		 * @param	e
		 */
		private function mySoundBtnClick(e:MouseEvent):void {
			var mc:MovieClip = e.currentTarget.parent as MovieClip;
			var no:uint = uint(mc.name.substr( -1));
			soundStop();
			if (record) record[no].listen();
			
		}
		
		/**
		 * 나의 답변/모범 답변 stop btn click
		 * @param	e
		 */
		private function soundStopBtnClick(e:MouseEvent):void {
			soundStop();
		}
		
		/**
		 * 모범 답변 btn click 
		 * @param	e
		 */
		private function ansBtnClick(e:MouseEvent):void {
			var mc:MovieClip = e.currentTarget.parent as MovieClip;
			var no:uint = uint(mc.name.substr( -1));
			soundStop();
			ansSound[no].soundPlay();
		}
		
		/**
		 * 나의 답변/모범 답변 stop
		 */
		private function soundStop():void {
			for (var i:uint = TSData.PART_QST_CNT[partNo - 1]; i < TSData.PART_QST_CNT[partNo]; i++) {
				var no:uint = i - TSData.PART_QST_CNT[partNo - 1];
				ansSound[no].soundStop();
				if (record) record[no].stopListen();
			}
		}
		
		/**
		 * 나의 답변/모범 답변 pause
		 */
		private function soundPause():void {
			for (var i:uint = TSData.PART_QST_CNT[partNo - 1]; i < TSData.PART_QST_CNT[partNo]; i++) {
				var no:uint = i - TSData.PART_QST_CNT[partNo - 1];
				if (record) record[no].stopListen();
				ansSoundSec[no] = ansSound[no].soundPause();
			}
		}
		
		
		/**
		 * 나의 답변/모범 답변 Mc click
		 * @param	e
		 */
		private function soundMcClick(e:MouseEvent):void {
			var mc:MovieClip = e.currentTarget.parent as MovieClip;
			var no:uint = uint(mc.name.substr( -1));
			var name:String = e.currentTarget.name.split("Sound")[0];
			
			soundPause();
			if (name == "my") {	//나의 답변
				record[no].listen();
				soundMcReset();
				mc.mySoundStopMc.gotoAndStop(1);
			}else if (name == "ans") {	//모범 답변
				if (e.currentTarget.currentFrame == 2) {	//처음부터 재생
					ansSound[no].soundGotoPlay(0);
				}else {	//이어서 재생
					ansSound[no].soundGotoPlay(ansSoundSec[no]);
				}
				soundMcReset();
				mc.ansSoundPauseMc.gotoAndStop(1);
			}
			
			e.currentTarget.gotoAndStop(2);
		}
			
		/**
		 * 나의 답변/모범 답변 pause mc click
		 * @param	e
		 */
		private function soundPauseMcClick(e:MouseEvent):void {
			var mc:MovieClip = e.currentTarget.parent as MovieClip;
			var no:uint = uint(mc.name.substr( -1));
			var name:String = e.currentTarget.name.split("Sound")[0];
			
			soundMcReset();
			e.currentTarget.gotoAndStop(2);
			soundPause();
		}
		
		/**
		 * 나의 답변/모범 답변 무비클립들 초기화
		 */
		private function soundMcReset():void {
			for (var i:uint = TSData.PART_QST_CNT[partNo - 1]; i < TSData.PART_QST_CNT[partNo]; i++) {
				var no:uint = i - TSData.PART_QST_CNT[partNo - 1];
				var mc:MovieClip = _root.mcRoot["q" + no] as MovieClip;
				mc.mySoundMc.gotoAndStop(1);
				mc.mySoundStopMc.gotoAndStop(1);
				mc.ansSoundMc.gotoAndStop(1);
				mc.ansSoundPauseMc.gotoAndStop(1);
			}
			
		}
		
		
		/**
		 * 다시풀기 버튼 클릭
		 * @param	e
		 */
		public function reStartBtnClick(e:MouseEvent):void {
			soundStop();
		}
		
	}

}